package com.junyue.control.platform.utils;

import com.alibaba.fastjson.JSONArray;
import com.junyue.control.platform.entity.ControlData;
import com.junyue.control.platform.entity.ResultData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommonUtils {
    /**
     * 写入文件并刷新缓存
     *
     * @param filePath
     * @param dataMap
     * @param controlDataList
     */
    public static void WriteFileAndRefresh(String filePath, Map<String, ResultData> dataMap, List<ControlData> controlDataList) {
        if (filePath == null || filePath.isEmpty())
            return;
        if (dataMap == null || dataMap.isEmpty())
            return;
        if (controlDataList == null || controlDataList.isEmpty())
            return;
        WriteFileUtils.createFile(JSONArray.toJSONString(controlDataList), filePath);
        FileLoader.getInstance(filePath).setDataMap(dataMap);
    }

    /**
     * MapToList
     *
     * @param dataMap
     * @return
     */
    public static List<ControlData> MapToList(Map<String, ResultData> dataMap) {
        if (dataMap != null && !dataMap.isEmpty()) {
            List<ControlData> controlDataList = new ArrayList();
            for (String key : dataMap.keySet()) {
                ControlData controlData = new ControlData();
                controlData.setVersionName(key);
                controlData.setResultData(dataMap.get(key));
                controlDataList.add(controlData);
            }
            return controlDataList;
        } else {
            return null;
        }
    }
}
